import scala.collection.mutable.Map
import scala.io.Source

class CheckSum {
    
  private var sum = 0
  
  def add(b : Byte) : Unit = sum += b;
  
  def checkSum() : Int = ~(sum & 0xFF) + 1
  
  def unary_+() : Int = {
    -sum
  }
  
  override def toString() = "sum: " + sum
  
  def fileLines(file: java.io.File) =
    scala.io.Source.fromFile(file).getLines().toList
  
  val filesHere = (new java.io.File(".")).listFiles()
  val fileLength = for {file <- filesHere if file.getName.endsWith(".scala");
      line <- fileLines(file) 
      trimmed = line.trim
      if trimmed.matches(".*for.*")
  } yield  {
	  println(file)
    trimmed.length
  }
}

object CheckSum {
  
  private val cache = Map[String, Int]()
  
  def calculate(s : String) : Int = {
    if (cache.contains(s))
        cache(s)
    else {
      val acc = new CheckSum
      for (b <- s) 
        acc.add(b.toByte)
      var cs = acc.checkSum()
      cache += (s -> cs)
      cs
    }
  }
  
  def processFile(fileName: String, width: Int) = {
    
    def processLine(fileName: String, width: Int, line: String) = 
      if (line.length >= width)
        "fileName: " + line
    
	  val lines = Source.fromFile(fileName).getLines().toList
	  
//	  val gtLines = for (line <- lines if (line.length >= width)) yield "fileName: " + line
	  val gtLines = for (line <- lines) yield processLine(fileName, width, line)
	  
	  gtLines.mkString("\n")
  }
    
}

